<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  ~ Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
  --%>

<%--
  Created by IntelliJ IDEA.
  User: inkux
  Date: 2018/5/24
  Time: 14:53
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<html>
<head>
	<title>Permission Group</title>
	<%@include file="../resources/js_css_includes.jsp" %>
	<style>
		.align_right {
			text-align: right;
		}
		#modify_form_wrapper_div{
			margin-left: 50px;
			width:500px;
		}
	</style>
	<script>
		function setCheckboxAttribute(checkbox, boolean) {
			if (boolean) {
				checkbox.attr("checked", "true");
			}
		}

		function setCheckboxAttributes() {
			setCheckboxAttribute($("#admin"), ${permissionGroup.admin});
			setCheckboxAttribute($("#m_user"), ${permissionGroup.modifyUser});
			setCheckboxAttribute($("#l_user"), ${permissionGroup.lookupUser});
			setCheckboxAttribute($("#m_dept"), ${permissionGroup.modifyDepartment});
			setCheckboxAttribute($("#l_dept"), ${permissionGroup.lookupDepartment});
			setCheckboxAttribute($("#m_desg"), ${permissionGroup.modifyDesignation});
			setCheckboxAttribute($("#l_desg"), ${permissionGroup.lookupDesignation});
			setCheckboxAttribute($("#m_deli"), ${permissionGroup.modifyDeliverman});
			setCheckboxAttribute($("#l_deli"), ${permissionGroup.lookupDeliverman});
			setCheckboxAttribute($("#m_cent"), ${permissionGroup.modifyCenter});
			setCheckboxAttribute($("#l_cent"), ${permissionGroup.lookupCenter});
			setCheckboxAttribute($("#deliver"), ${permissionGroup.useDeliverSystem});
		}

		$(document).ready(function () {
			setCheckboxAttributes();
			$("#grp_name").change(function () {
				$.ajax({
					url: "ajax/check_permission_group_name",
					data: {
						except: "${permissionGroup.groupName}",
						groupName: $("#grp_name").val()
					},
					type: "post",
					dataType: "json",
					success: function (data) {
						var exists = data.exists;
						if (exists) {
							$("#grp_name").css("border", "1px solid red");
						} else {
							$("#grp_name").css("border", "1px solid green");
						}
					},

					error: function (jqHXR, textStatus, error) {
					}
				});
			});
			$("#modify_form").submit(function () {
				$("#modify_form").ajaxSubmit({
					url: "modify_permission_group",
					target: null,
					type: "post",
					dataType: "json",
					success: function (data) {
						if (data.status == "success") {
							showMessagePopup("Success", "Permission group successfully updated.",
								function () {
									window.top.close();
								});
						} else {
							showErrorPopup("Error", data.errorMessage);
						}
					},

					error: function (jqHXR, textStatus, error) {
						showErrorPopup("Error", "Could not connect to server");
					}
				});
				return false;
			});
		});
	</script>
</head>
<body>
<%@include file="../resources/navigation_header.jsp"%>
<%@include file="../resources/jsp_body_preludes.jsp" %>
<div class="table_div_forall">
<c:choose>
	<c:when test="${permissionGroup != null}">
		<form id="modify_form" action="" method="post">
			<table id="modify_form_wrapper_div">
			<tr>
				<td><label for="grp_id">Group ID</label></td>
				<td colspan="3"><input class="input" readonly id="grp_id" name="id" type="text"
				           value="${permissionGroup.id}"></td>
			</tr>
			<tr>
				<td><label for="grp_name">Name</label></td>
				<td colspan="3"><input class="input" id="grp_name" name="groupName" type="text"
				           value="${permissionGroup.groupName}"></td>
			</tr>
			<tr>
				<td class="align_right"><input id="admin" name="admin" type="checkbox"></td>
				<td><label for="admin">Perm. Admin</label></td>
			</tr>
			<tr>
				<td class="align_right"><input id="m_user" name="modifyUser" type="checkbox"></td>
				<td><label for="m_user">Modify User</label></td>
				<td class="align_right"><input id="l_user" name="lookupUser" type="checkbox"></td>
				<td><label for="l_user">Lookup User</label></td>
			</tr>
			<tr>
				<td class="align_right"><input id="m_dept" name="modifyDepartment" type="checkbox"></td>
				<td><label for="m_dept">Modify Dept.</label></td>
				<td class="align_right"><input id="l_dept" name="lookupDepartment" type="checkbox"></td>
				<td><label for="l_dept">Lookup Dept.</label></td>
			</tr>
			<tr>
				<td class="align_right"><input id="m_desg" name="modifyDesignation" type="checkbox"></td>
				<td><label for="m_desg">Modify Desg.</label></td>
				<td class="align_right"><input id="l_desg" name="lookupDesignation" type="checkbox"></td>
				<td><label for="l_desg">Lookup Desg.</label></td>
			</tr>
			<tr>
				<td class="align_right"><input id="m_deli" name="modifyDeliverman" type="checkbox"></td>
				<td><label for="m_deli">Modify Delivery Men</label></td>
				<td class="align_right"><input id="l_deli" name="lookupDeliverman" type="checkbox"></td>
				<td><label for="l_deli">Lookup Delivery Men</label></td>
			</tr>
			<tr>
				<td class="align_right"><input id="m_cent" name="modifyCenter" type="checkbox"></td>
				<td><label for="m_cent">Modify Centers</label></td>
				<td class="align_right"><input id="l_cent" name="lookupCenter" type="checkbox"></td>
				<td><label for="l_cent">Lookup Centers</label></td>
			</tr>
			<tr>
				<td class="align_right"><input id="deliver" name="useDeliverSystem" type="checkbox"></td>
				<td><label for="deliver">Use Deliver System</label></td>
			</tr>
			<tr>
				<td></td>
				<td colspan="3"><input class="btn_color2" type="submit" id="submit_button" value="Update"/></td>
			</tr>
			</table>

		</form>
	</c:when>
	<c:otherwise>
		<div style="color: red;">${errorMessage}</div>
	</c:otherwise>
</c:choose>
</div>
</body>
</html>
